<?
session_start();
require_once 'php/functions.php';
require_once 'php/connect.php';

$sess_name = session_name();

$userName =  mysql_real_escape_string($_POST['userName']);

if(isset($userName)){

	$isOwn = false;
	$foundUser = false;
	
	if(isset($_SESSION['email']) && isset($_SESSION['password']) && isset($_SESSION['userId'])){
		if($userName == "me" || $userName == $_SESSION['userName']){
			$userName = $_SESSION['userName'];
 			$isOwn = true;
			$foundUser = true;
		}
	}
	
	if(!$isOwn){
		$abfrage = "SELECT user.userId
				   ,user.picture
				   ,user.userName
				   ,user.avatar
				   ,user.posts
				   ,user.icq
				   ,user.msn
				   ,user.cdate
				   ,user.gameCharacter
				   ,user.gameServer
			FROM 
			ffxivc_users as user
			WHERE user.userName = '".$userName."' ";

		$res = mysql_query($abfrage);
		$num = mysql_num_rows($res);	
		if ($num == 1){
			$foundUser = true;
			$uId = mysql_result($res, 0, "userId");
			$pic = mysql_result($res, 0, "picture");
			$name = mysql_result($res, 0, "userName");
			$cd = mysql_result($res, 0, "cdate");
			$pos = mysql_result($res, 0, "posts");
			$char = mysql_result($res, 0, "gameCharacter");
			$server = mysql_result($res, 0, "gameServer");
			$icq = mysql_result($res, 0, "icq");
			$msn = mysql_result($res, 0, "msn");
		}
	}
		
	
	$output = "
	<html>
		<head>	 	
			<!-- User Window -->

		</head>
		<body>
		<script>
			$(document).ready(function(){
				$('.show-user').click(function(e) {	
					var clicked = jQuery(e.target);
					var id = clicked.attr('id');
					getUser(id);
				});
				$('.send-mail-pop').click(function(e) {	
					showSendMessage(1);
				});
				$(function() {
					$('#send_message_popup').draggable({cancel: '#send_message_popup_body'});
				});
				showSendMessage(0);
			});
			function showSendMessage(toggle) {
				if(toggle=='1'){
					$('#send_message_popup').load('php/send_message.php', {uName: \"".$userName."\", action: 'SHOW'});
					$('#send_message_popup').fadeIn('fast');
				}else{
					$('#send_message_popup').fadeOut('fast');
					
				}
			};
			
				
		</script>
		<div id='send_message_popup'></div>";
		
	if(!$foundUser){
		$output .= "User not found!";
	}else{
		$output .= "<div class = 'content-title-container'>
				<div id='".$userName."' class='show-user cur-point normal-text blue-text content-title-text fl-left' >".$userName."</div><div class='normal-text grey-text content-title-text fl-left' style='margin-left: 5px;'> 's home</div>";
	
		$newMsg = getNewMessages();
		if($newMsg == 0){
			$newMsg = "";
		}else{
			$newMsg = " (".$newMsg.")";
		}
		if($isOwn){
			$output .= "<ul id='sub-menu-item-list'><li class='menu-item bold-text menu-item-selected'>Specs</li><li class='menu-item bold-text'>Profile</li><li class='menu-item bold-text'>Messages".$newMsg."</li></ul>
						<script>
							enableMenu();
						</script>";
		}else if(isset($_SESSION['userName'])){
			$output .= "<div class='fl-right cur-point'><img class='cur-point send-mail-pop' src='media/email.png' style='width: 16px;height: 16px;padding-top: 6px;padding-right: 10px;opacity:0.7;' /></div>";
		}
			
		$output .= "</div>";
		
		if($pic != ""){
			$MyPicture = "<img id='profile_pic' class='user-picture' src='".$pic."'/>";
		}else{
			$MyPicture = "<img id='profile_pic' src='media/unknown_n.png' class='marg-auto'/>";
		}
		if(!$isOwn){
			$output .= 	"<div class='user-content-container pad-t-20'>
							<div class='content-subtitle-container-open'></div>
							<div>
								<div class='fl-left'>
									<div class='user-information fl-left'>
										<div class='border-pic'>
											".$MyPicture."
										</div>
									</div>
									<div class='fl-right'>
										<div class='fl-left'>
											<table>
												<tr>
													<td class='normal-text blue-text bold-text pad-10'>
														Name:
													</td>
													<td class='normal-text grey-text bold-text pad-10'>
														".$name."
													</td>
												</tr>
												<tr>
													<td class='normal-text blue-text bold-text pad-10'>
														Posts:
													</td>
													<td class='normal-text grey-text bold-text pad-10'>
														".$pos."
													</td>
												</tr>
												<tr>
													<td class='normal-text blue-text bold-text pad-10'>
														Joined:
													</td>
													<td class='normal-text grey-text bold-text pad-10'>
														".$cd."
													</td>
												<tr>
											</table>
										</div>
										<div class='fl-right'>
											<table>
												<tr>
													<td class='normal-text blue-text bold-text pad-10'>
														Char:
													</td>
													<td class='normal-text grey-text bold-text pad-10'>
														".$char."
													</td>
												</tr>
												<tr>
													<td class='normal-text blue-text bold-text pad-10'>
														Server:
													</td>
													<td class='normal-text grey-text bold-text pad-10'>
														".$server."
													</td>
												<tr>
											</table>
										</div>
									</div>
									<div style='clear:both;'></div>
								</div>
								<div style='clear:both;'></div>
							</div>
							<div class = 'content-subtitle-container-close'></div>
						</div>
						";
		}
		
		$output .= 	"<div id='indent-content-container' class='me-content-container'";
		if(!$isOwn){
			$output .= 	" style='padding-top: 5px;'";
		}
		
		$output .= 	"> 
						<div id='spec-detail' class='fl-left'>
							<div class = 'content-subtitle-container-left fl-left'></div>
							<div class='fl-right'>
								<div id='spec-detail-content' class='fl-left'></div>
								<div class = 'content-subtitle-container-right fl-right'></div>
							</div>
						</div>
						<div id='spec-list-container' class='fl-right width-390'>
							<div class = 'content-subtitle-container-open'></div>
							<div id = 'spec-list' class='spec-list'></div>
							<script>";
			if($isOwn){
				$output .= "loadSpecList('');";
			}else{
				$output .= "loadSpecList('".$userName."');";
			}
				$output .= "
							</script>
							<div style='clear:both; height:5px;'></div>
							<div class = 'content-subtitle-container-close'></div>
						</div>
						<div style='clear:both; height:5px;'></div>
						<div class = 'content-subtitle-container-open'></div>
						<div id='spec-comments-out'>
							<div id='spec-comments' class='spec-comments'>
							</div>
						</div>
						<div class = 'content-subtitle-container-close'></div>
					</div>
				</body>
			</html>";
	}
	
	
}
		
echo $output;
?>


		
